package cn.wdm.ymcc.rocketmq.consummer;

import cn.wdm.ymcc.service.IPayOrderService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 订单支付超时 支付单处理及调支付宝关闭支付的处理
 * MessageExt 里面是要消费的消息Message CourseNo订单号
 */
@Component
@RocketMQMessageListener(
        consumerGroup="payorder-timeout-message-consummer",
        topic="pay-timeout-message-topic",
        selectorExpression="pay-timeout-message-tag",
        messageModel= MessageModel.BROADCASTING //默认是集群模式，一个消息只会被一个消费者消费
)
public class PayTimeOutHandleMessageConsummer implements RocketMQListener<MessageExt> {
    @Autowired
    private IPayOrderService payOrderService;
    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();
        if(body == null || body.length == 0){
            return;//消息不对，直接签收
        }
        String courseOrderNo = new String(body, StandardCharsets.UTF_8);
        payOrderService.cancelPayOrder(courseOrderNo);
    }
}
